package io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 文件字符流输入类测试
 */
public class FileReaderTest {
    public static void main(String[] args) {
        readOneChar();
//       readChars();
    }

    /**
     * 读取单个字符
     */
    static void readOneChar() {
        FileReader fr = null;
        try {
            fr = new FileReader("/Users/luchuan/IdeaProjects/java-test/a.txt");
            int len;
            while ((len = fr.read()) != -1) {
                System.out.println((char) len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 读取多个字符
     */
    static void readChars() {
        FileReader fr = null;
        try {
            fr = new FileReader("/Users/luchuan/IdeaProjects/java-test/a.txt");
            char[] chars = new char[1024];
            int len;
            while ((len = fr.read(chars)) != -1) {
                System.out.println(new String(chars, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
